package com.v2future.sjt.collegecheckingattendance.rollcall;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.leave.bean.TeacherBean;
import com.v2future.sjt.collegecheckingattendance.rollcall.adapter.CourseAdapter;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.CourseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 点名选择课程
 * Created by Administrator on 2016/10/12.
 */

public class SelectCourseActivity extends MainActivity implements
        PullToRefreshView.OnHeaderRefreshListener,
        PullToRefreshView.OnFooterRefreshListener,
        AdapterView.OnItemClickListener{

    private PullToRefreshView pullView;

    private ListView courseLv;

    private CourseAdapter courseAdapter;

    //老师
    private TeacherBean teacherBean;

    public static String TAG_COURSE = "course";

    /**
     * 课程列表
     */
    private CourseBean courseBean;

    @Override
    public void init() {
        teacherBean = (TeacherBean) getIntent().getSerializableExtra(IntentKey.TEACHER);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_rollcall_course);
        initTitleBack(null);
        if (null != teacherBean) {
            setTitleText(teacherBean.getUserName());
        }
        else {
            setTitleText("选择课程");
        }
        courseLv = (ListView) findViewById(R.id.lv_course);
        courseLv.setOnItemClickListener(this);
        pullView = (PullToRefreshView) findViewById(R.id.pullView);
        pullView.setOnHeaderRefreshListener(this);
        pullView.setOnFooterRefreshListener(this);
        pullView.setFooterViewVisibility(View.GONE);
    }

    @Override
    public void initAfter() {
        showTitleLoading();
        doGetCourse(teacherBean.getUserCode());
    }

    /**
     * 获取老师名下课程
     * @param teacherId
     */
    private void doGetCourse(final String teacherId){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.TEACHER_COURSE));
        params.removeUserCode();
        params.addQueryStringParameter("userCode", teacherId);
        NetUtils.doGetRequest(this, params, new NetTaskListener<CourseBean>() {

            @Override
            public void onSuccess(CourseBean result) {
                if (result.getData().size() <= 0) {
                    tip("没有课程");
                    return;
                }
                courseBean = result;
                Collections.sort(courseBean.getData(), comparator);
                courseAdapter = new CourseAdapter(SelectCourseActivity.this, result.getData());
                courseLv.setAdapter(courseAdapter);
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(CourseBean result) {
                tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                cancelTitleLoading();
                pullView.onHeaderRefreshComplete();
                pullView.onFooterRefreshComplete();
            }

            @Override
            public void onSuccessCache(CourseBean result) {

            }
        });
    }

    Comparator<CourseBean.DataBean> comparator = new Comparator<CourseBean.DataBean>() {
        @Override
        public int compare(CourseBean.DataBean b1, CourseBean.DataBean b2) {
            Integer week1 = Integer.parseInt(b1.getWeekDay());
            Integer week2 = Integer.parseInt(b2.getWeekDay());
            Integer course1 = Integer.parseInt(b1.getCourseNumber());
            Integer course2 = Integer.parseInt(b2.getCourseNumber());
            int weekResult = week1.compareTo(week2);
            int courseResult = course1.compareTo(course2);
            if (Math.abs(weekResult) > 0) {//绝对值判断
                return weekResult;
            }
            else {
                return courseResult;
            }
        }
    };

    @Override
    public void onFooterRefresh(PullToRefreshView view) {

    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        doGetCourse(teacherBean.getUserCode());
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        CourseBean.DataBean dataBean = courseBean.getData().get(i);
        dataBean.setTeacherId(teacherBean.getUserId());//放入老师的id，防止老师和课程无法对应
        Intent backIntent = new Intent(SelectCourseActivity.this, HomeActivity.class);
        backIntent.setAction(RollCallFragment.TAG_ROLL_CALL_BACK_COURSE);
        backIntent.putExtra(TAG_COURSE, dataBean);
        backIntent.putExtra("id", HomeActivity.FLAG_ROLLCALL);
        gobackWithData(backIntent);
    }
}
